<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/modules/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/modules/employees'

export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: [] // 获取所有角色列表
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 为用户分配角色
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.roleId, roleIds: this.roleIds })
        console.log('assignRoles', res)
        this.$emit('close')
        // 保存数据
      } catch (err) {
        console.log('assignRoles', err)
      }
    },
    // 获取所有角色列表
    async loadRoles() {
      try {
        const res = await getRoles()
        console.log('getRoles', res)
        this.list = res.data.rows
        const info = await getUserDetailById(this.roleId)
        console.log(info, 'info')
        this.roleIds = info.data.roleIds || ''
        // 保存数据
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
